package cn.com.qtgl.tests;

import cn.com.qtgl.oauth2.ClientType;
import cn.com.qtgl.oauth2.annotation.Token;
import cn.com.qtgl.sso.oidc.comp.config.SsoConfig;
import cn.com.qtgl.sso.oidc.comp.config.SsoScopesConfig;
import cn.melonlib.cores.aop.comp.web.WebInfo;
import cn.melonlib.cores.commons.comp.web.ctrl.BasicController;
import cn.melonlib.cores.commons.comp.web.ctrl.IndexController;
import cn.melonlib.cores.graphql.comp.web.proxy.GraphqlWebProxy;
import cn.melonlib.cores.graphql.comp.web.utils.GraphqlArgumentsUtils;
import cn.melonlib.cores.jpa.model.bean.collections.JsonMap;
import cn.melonlib.cores.security.auth2.comp.config.ApiConfig;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

@RestController
@RequestMapping(path = "/")
public class MainController extends BasicController {

    @Resource
    private GraphqlWebProxy graphqlWebProxy;

//    @Resource
//    private FileApiController fileApiController;

    @Resource
    private IndexController indexController;

    @Value("${server.servlet.context-path}")
    private String base;



    @Resource
    private SsoConfig ssoConfig;

    @Resource
    private SsoScopesConfig ssoScopesConfig;
    @Resource
    private ApiConfig apiConfig;

    @Resource
    private WebInfo webInfo;

    @Resource
    private GraphqlArgumentsUtils graphqlArgumentsUtils;

    @CrossOrigin(originPatterns = "*")
    @ResponseBody
    @RequestMapping(value = "provider", method = {RequestMethod.POST}, produces = APPLICATION_JSON_UTF8_VALUE)
    @Token(scope = "data,openid,profile,storage,account", require = true, clientType = ClientType.ALL)
    public Object provider( HttpServletRequest request) throws Exception {
        return graphqlWebProxy.executeGraphQL(graphqlArgumentsUtils.getArguments(), request);
    }

    @GetMapping(value = "provider.api", produces = APPLICATION_JSON_UTF8_VALUE)
    public String providerApi(HttpServletRequest request, HttpServletResponse response)throws Exception{
        return graphqlWebProxy.providerMap(request,response);
    }

//    @PutMapping(path = "upload.api", produces = APPLICATION_JSON_UTF8_VALUE)
//    @Token(scope = "data,openid,profile,storage,account", require = true, clientType = ClientType.ALL)
//    public String upload(MultipartHttpServletRequest request) throws Exception {
//        return this.fileApiController.upload(request);
//    }

    @GetMapping(path = "index.html",produces = {"text/html"})
    public String index() throws Exception{
        String domain=webInfo.getServerInfo().getPrefix().replaceAll("/\\s*$","");
        return indexController.index("index",JsonMap.<Object>createMap()
                        .append("base",base)
                        .append("authority",domain+ssoConfig.getOauthAuthorityUrl())
                        .append("domain",domain)
                        .append("engieUrl",domain+"/infoplus")
                        .append("clientId",ssoConfig.getOauthClientId())
                        .append("secret",ssoConfig.getOauthClientSecret())
                        .append("scopes",ssoScopesConfig.getImplicit())
                        .append("builtin",apiConfig.getBuiltin())
                        .append("provider",base+"/provider")
                        .append("graphql_public",base+"/graphql_public")
                        .append("system_name","办公自动化系统"));
    }
}
